<?php

class ElementsPerPage extends Data
{
    protected $htmlOptions=array(
        'elementId'=>'data-count-selector',
        'class'=>'selector button',
        'dataId'=>'',
        'elementName'=>'',
        'selectCount'=>0,
        'elementsCount'=>0,
    );
    protected $params=array();

    public function __construct($params=array())
    {
        if($params) $this->setParams($params);
    }

    public function getElements($params=array())
    {
        if($params) $this->setParams($params);
        $params=$this->params;

        $elementsCount=$this->htmlOptions['elementsCount'] ? $this->htmlOptions['elementsCount'] : Sys::app()->request->get('elementsCount');
        if(!$elementsCount){
            Sys::app()->error='Не передан обязательный параметр "elementsCount" - количество элементов. Завершена работа обработчика данных ElementsPerPage';
            return false;
        }

        $selectCount=$this->htmlOptions['selectCount'] ? $this->htmlOptions['selectCount'] : (Sys::app()->request->get('selectCount') ? Sys::app()->request->get('selectCount') : Sys::app()->config->data->elementsSelectCount);
        if($selectCount>=$elementsCount) return false;

        $limit=Paging::getLinesPerPage($params);
        $start=$limit['start'];
        $limit=$limit['limit'];
        if($start>0) $params['limit']=$start.', '.$limit;
        else $params['limit']=$limit;
        $this->params=$params;

        $currentRoute=Sys::app()->request->route;
        $countStep=ceil($elementsCount/$selectCount);

        $options=array();
        for($i=1;$i<=$selectCount;$i++){
            if($i==1){
                $option=new stdClass();
                $option->value=$limit;
                $option->dataId=Sys::app()->request->createUrl($currentRoute,$params);
                $options[]=$option;
            }

            $elementsPerPage=$i*$countStep;

            if($limit!=$elementsPerPage){
                $option=new stdClass();
                if($elementsPerPage>$elementsCount){
                    $elementsPerPage=$elementsCount;

                }
                if($start>0) $params['limit']=$start.', '.$elementsPerPage;
                else $params['limit']=$elementsPerPage;
                $option->value=$elementsPerPage;
                $option->dataId=Sys::app()->request->createUrl($currentRoute,$params);

                $options[]=$option;
            }
        }

        if($view=$this->checkView()){
            $data=array(
                'divId'=>'elements-per-page-selector',
                'divClass'=>'data',
                'class'=>isset($params['class']) ? $params['class'] : $this->htmlOptions['class'],
                'id'=>isset($params['elementId']) ? $params['elementId'] : $this->htmlOptions['elementId'],
                'name'=>'elements-per-page',
                'options'=>$options,
            );

            return $this->renderView($view,$data);
        }

        return false;
    }

    public function getAttributes()
    {
        $limit=self::getLinesPerPage(array_merge(Sys::app()->request->params,$this->params));
        $limit=isset($limit['limit']) && $limit['limit'] ? $limit['limit'] : Sys::app()->config->paging->dataLimit;

        $newSort=array('limit'=>$limit);
        if($this->params) return array_merge($newSort,$this->params);
        else return $newSort;
    }
}